package com.byCreatetion.adabter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import com.byCreation.t_globe.R;
import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.nostra13.universalimageloader.core.decode.BaseImageDecoder;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.squareup.picasso.Picasso;





import YouTubeApi.CallYouTubeApi;
import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.AsyncTask;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;




public class HistoryAdapter extends BaseAdapter {
    
	
    private Activity activity;
    private LayoutInflater inflater=null;
  //  public ImageLoader imageLoader; 
  
    ArrayList<HashMap<String, Object>> MyArrList = new ArrayList<HashMap<String, Object>>();
    
    
    private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
	 private ImageLoader imageLoader = ImageLoader.getInstance();
	 private DisplayImageOptions options;
	 
	 
	 private int[] id_h={R.drawable.h1,R.drawable.h2,R.drawable.h3,R.drawable.h4};
	
	 
    
    public HistoryAdapter(Activity a,ArrayList al ) {
    	
    	activity = a;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        MyArrList=al;
       
    
    
        options = new DisplayImageOptions.Builder()
		.showStubImage(R.drawable.ic_launcher)
		.showImageForEmptyUri(R.drawable.ic_launcher)
		.showImageOnFail(R.drawable.ic_launcher)
		.delayBeforeLoading(100)
        .cacheInMemory()
		.cacheOnDisc()
		.bitmapConfig(Bitmap.Config.RGB_565)
		.displayer(new RoundedBitmapDisplayer(0))
		.build();
	initImageLoader(activity.getApplicationContext());
    
    }
    
    private class ViewHolder {
    	private TextView title,subscriberCount,updated,txt_newvdo,txt_title,txt_date;
		private ImageView thumbnail,img_bg;
		private LinearLayout lay_newvdo;
	}


   
    @Override
	public int getCount() {
        return MyArrList.size();
    }
    @Override
    public Object getItem(int position) {
        return position;
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    
    public View getView(int position, View convertView, ViewGroup parent) {
      
    	View vi=convertView;
    	final ViewHolder holder;
    	
        if(convertView==null)
        {
	        vi = inflater.inflate(R.layout.item_history_listview, null);
	        holder = new ViewHolder();
		    holder.thumbnail = (ImageView)vi.findViewById(R.id.img_list);
		    holder.subscriberCount= (TextView)vi.findViewById(R.id.txt_subscriberCount);
		    holder.img_bg= (ImageView)vi.findViewById(R.id.img_bg);
		    holder.txt_title=(TextView)vi.findViewById(R.id.txt_title);
		    holder.txt_date=(TextView)vi.findViewById(R.id.txt_date);
		    
		    
		    
			vi.setTag(holder);	
	 	
	 		
	 		
	
	 		
	}else 
    {
	
    		holder = (ViewHolder)vi.getTag();
    }
            
       /* 
        if(position%2==0)
        	holder.img_bg.setBackgroundColor(Color.parseColor("#aaffffff"));
        else
        	holder.img_bg.setBackgroundColor(Color.parseColor("#ccffffff"));*/

        
        holder.subscriberCount.setText(MyArrList.get(position).get("detail").toString());
        holder.txt_title.setText(MyArrList.get(position).get("title").toString());
        holder.txt_date.setText(MyArrList.get(position).get("visit_date").toString());
        
        //=================================SetData===================================
      
	

        Picasso.with(activity)
        .load(MyArrList.get(position).get("img").toString())
        .placeholder(R.drawable.ic_launcher)
        .error(R.drawable.ic_launcher)
        .into( holder.thumbnail);
        
	
	return vi;
    }
    
    
    public  void initImageLoader(Context context) {
  		// This configuration tuning is custom. You can tune every option, you may tune some of them, 
  		// or you can create default configuration by
  		//  ImageLoaderConfiguration.createDefault(this);
  		// method.
  		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
		  		.memoryCacheExtraOptions(100, 200) // default = device screen dimensions
		        .threadPoolSize(2) // default
		        .threadPriority(Thread.NORM_PRIORITY - 1) // default
		        .tasksProcessingOrder(QueueProcessingType.FIFO) // default
		        .denyCacheImageMultipleSizesInMemory()
		        .memoryCache(new LruMemoryCache(2 * 1024 * 1024))
		        .memoryCacheSize(2 * 1024 * 1024)
		        .discCacheSize(50 * 1024 * 1024)
		        .discCacheFileCount(100)
		        .discCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default
		        .imageDownloader(new BaseImageDownloader(context)) // default
		       
		       
		        .build();
  		// Initialize ImageLoader with configuration.
  		ImageLoader.getInstance().init(config);
  	}
  	
    
  	@SuppressWarnings("unused")
	private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {

		 final static List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());

		@Override
		public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
			if (loadedImage != null) {
				ImageView imageView = (ImageView) view;
				boolean firstDisplay = !displayedImages.contains(imageUri);
				if (firstDisplay) {
					FadeInBitmapDisplayer.animate(imageView, 500);
					displayedImages.add(imageUri);
				}
			}
		}
	}
    
    
}